package com.dji.ux.sample.TCPClient;

import android.util.Log;

import com.dji.ux.sample.Weather.WeatherParams;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TCPDrone {

    OnPLCStatusCallBackBlock plcStatusCallBackBlock;
    OnWeatherParamsCallBack weatherParamsCallBack;
    TCPClient mTcpClient;
    Thread PLCReceiver;
    PLCCommand plcCommand;
    String ipAddress;
    int port;
    private boolean isTCPConnect = false;

    public TCPDrone(String ipAddress, int port) {
        plcCommand = new PLCCommand();
        mTcpClient = TCPClient.sharedCenter();
//        mTcpClient.connect("192.168.2.250", 20000);
//        mTcpClient.connect("192.168.137.1", 20000);
        mTcpClient.connect(ipAddress, port);
        initHeartTimer();
        PLCReceiver = new Thread(new Runnable() {
            @Override
            public void run() {
                Timer timer = new Timer();
                TimerTask timerTask = new TimerTask() {
                    @Override
                    public void run() {
                        if(isTCPConnect){
                            mTcpClient.send(PLCCommand.get_plcdata);
                            mTcpClient.send(PLCCommand.get_weather);
                        }
                    }
                };
                timer.schedule(timerTask, 0, 200);
            }
        });
        mTcpClient.setReceivedCallback(new TCPClient.OnReceiveCallbackBlock() {
            @Override
            public void callback(final ArrayList<PLCStatus> receicedMessage) {
                plcStatusCallBackBlock.callback(receicedMessage);
                Log.d("TCPDrone--", receicedMessage.toString());
            }
        });
        mTcpClient.setWeatherParamListener(new TCPClient.OnWeatherParamListener() {
            @Override
            public void callback(WeatherParams param) {
                weatherParamsCallBack.callback(param);
                Log.d("TCPDrone--Weather", param.toString());
            }
        });
        mTcpClient.setConnectedCallback(new TCPClient.OnServerConnectedCallbackBlock() {
            @Override
            public void callback() {
                isTCPConnect = true;
                PLCReceiver.start();
            }
        });
        mTcpClient.setDisconnectedCallback(new TCPClient.OnServerDisconnectedCallbackBlock() {
            @Override
            public void callback(IOException e) {
                isTCPConnect = false;
            }
        });

    }

    private void initHeartTimer(){
        if (plcCommand != null){
            new Runnable(){
                @Override
                public void run() {
                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            mTcpClient.send(plcCommand.nextTickCommand());
                        }
                    }, 0, 100);
                }
            }.run();
        }
    };

    public void sendCommand(final byte[] data){
            plcCommand.setNowCommand(data);
            Log.d("SENDTOPLC____", "change");
    };

    public interface OnPLCStatusCallBackBlock{
        void callback(ArrayList<PLCStatus> receicedMessage);
    }

    public interface OnWeatherParamsCallBack{
        void callback(WeatherParams param);
    }

    public void setWeatherParamsCallBack(OnWeatherParamsCallBack weatherParamsCallBack) {
        this.weatherParamsCallBack = weatherParamsCallBack;
    }

    public void setPLCStatusCallBack(OnPLCStatusCallBackBlock plcStatusCallBackBlock) {
        this.plcStatusCallBackBlock = plcStatusCallBackBlock;
    }
}
